package com.athuahua.spzx.controller;

import com.athuahua.spzx.dto.h5.OrderInfoDto;
import com.athuahua.spzx.entity.OrderInfo;
import com.athuahua.spzx.service.OrderInfoService;
import com.athuahua.spzx.utils.Result;
import com.athuahua.spzx.vo.CartInfoVo;
import com.athuahua.spzx.vo.h5.TradeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: huahua
 * @Date: 2024/8/17 0017
 * @Time: 22:53
 * @Description:
 */
@Tag(name = "订单结算模块接口")
@RestController
@RequestMapping("api/order/orderInfo")
@Slf4j
public class OrderInfoController {
    @Resource
    private OrderInfoService orderInfoService;

    //返回购物车选中的商品 在cart模块中远程调用得到
    @GetMapping("auth/trade")
    public Result getTradeInfo() {
        log.info("getTradeInfo");
        TradeVo tradeInfo = orderInfoService.getTradeInfo();
        return Result.success(tradeInfo);
    }


    /**
     * 生成订单
     * order_info 订单基本信息表
     * order_item 订单项表 每个订单商品对应一条记录
     * order_log 订单日志表
     */
    @PostMapping("auth/submitOrder")
    public Result<Long> submitOrder(@RequestBody OrderInfoDto orderInfoDto) {
        log.info("submitOrder");
        Long l = orderInfoService.submitOrder(orderInfoDto);


        return Result.success(l);
    }

    /**
     * 获取订单数据
     *
     * @param orderId
     * @return
     */
    @GetMapping("auth/{orderId}")
    public Result<OrderInfo> getOrderInfo(@PathVariable("orderId") Long orderId) {
        log.info("getOrderInfo");
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderId);

        return Result.success(orderInfo);
    }

    /**
     * 立即购买
     */
    @GetMapping("auth/buy/{skuId}")
    public Result buyNow(@PathVariable("skuId") Long skuId) {
        TradeVo tradeVo = orderInfoService.buy(skuId);

        return Result.success(tradeVo);
    }

    /**
     * 全部订单查询
     */

    @GetMapping("auth/{page}/{limit}")
    public Result getAllOrderInfo(
            @PathVariable("page") Integer page,
            @PathVariable("limit") Integer limit,
            @RequestParam(name = "orderStatus", required = false, defaultValue = "") Integer orderStatus
    ) {
        IPage<OrderInfo> list = orderInfoService.getAllOrderInfo(page, limit, orderStatus);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list.getRecords());
        map.put("total", list.getTotal());

        return Result.success(map);
    }


    /**
     * openfeign 远程调用接口：
     * 根据订单编号获取订单信息
     */

    @GetMapping("getOrderInfoByOrderNo/{orderNo}")
    public OrderInfo getOrderInfoByOrderNo(@PathVariable("orderNo") String orderNo) {
        OrderInfo one = orderInfoService.getOrderInfoByOrderNo(orderNo);

        return one;

    }

    /**
     * openfeign 远程调用接口：
     * 更新订单状态
     */
    @GetMapping("updateOrderStatusPayed/{orderNo}/{orderStatus}")
    public Result updateOrderStatus(
            @PathVariable("orderNo") String orderNo,
            @PathVariable("orderStatus") Integer orderStatus
    ) {
        orderInfoService.updateOrderStatus(orderNo, orderStatus);


        return Result.success();
    }

}
